package com.ruoyi.shop.controller;

import java.util.List;

import com.ruoyi.shop.domain.ShopUserOrderItem;
import com.ruoyi.shop.service.IShopUserOrderItemService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.shop.domain.ShopUserOrder;
import com.ruoyi.shop.service.IShopUserOrderService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 订单Controller
 * 
 * @author Alan Zhang
 * @date 2021-09-14
 */
@Controller
@RequestMapping("/shop/order")
public class ShopUserOrderController extends BaseController
{
    private String prefix = "shop/order";

    @Autowired
    private IShopUserOrderService shopUserOrderService;
    @Autowired
    private IShopUserOrderItemService shopUserOrderItemService;

    @RequiresPermissions("shop:order:view")
    @GetMapping()
    public String order()
    {
        return prefix + "/order";
    }

    /**
     * 查询订单列表
     */
    @RequiresPermissions("shop:order:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(ShopUserOrder shopUserOrder)
    {
        startPage();
        List<ShopUserOrder> list = shopUserOrderService.selectShopUserOrderList(shopUserOrder);
        return getDataTable(list);
    }

    /**
     * 导出订单列表
     */
    @RequiresPermissions("shop:order:export")
    @Log(title = "订单", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(ShopUserOrder shopUserOrder)
    {
        List<ShopUserOrder> list = shopUserOrderService.selectShopUserOrderList(shopUserOrder);
        ExcelUtil<ShopUserOrder> util = new ExcelUtil<ShopUserOrder>(ShopUserOrder.class);
        return util.exportExcel(list, "订单数据");
    }


    /**
     * 订单备注
     */
    @RequiresPermissions("shop:order:edit")
    @Log(title = "订单备注", businessType = BusinessType.UPDATE)
    @PostMapping("/edit/remark")
    @ResponseBody
    public AjaxResult editRemark(Long id, String remark)
    {
        return toAjax(shopUserOrderService.updateShopUserOrderRemark(id, remark));
    }

    /**
     * 删除订单
     */
    @RequiresPermissions("shop:order:remove")
    @Log(title = "订单", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(shopUserOrderService.deleteShopUserOrderByIds(ids));
    }


    /**
     * 查看订单
     */
    @GetMapping("/view/{id}")
    public String view(@PathVariable("id") Long id, ModelMap mmap)
    {
        ShopUserOrder shopUserOrder = shopUserOrderService.selectShopUserOrderById(id);

        List<ShopUserOrderItem> orderItemList = shopUserOrderItemService.selectShopUserOrderItemByOrderId(id);
        shopUserOrder.setShopUserOrderItemList(orderItemList);

        mmap.put("shopUserOrder", shopUserOrder);

        return prefix + "/view";
    }
}
